class Solution {
public:
    int minMutation(string startGene, string endGene, vector<string>& bank) {
        unordered_set<string> bankHash(bank.begin(), bank.end());
        unordered_set<string> vis;
        string chargeArr = "ACGT";
        if(startGene == endGene)
            return 0;
        if(!bankHash.count(endGene))
            return -1;
 
        int ret = 0;
        queue<string> q;
        q.push(startGene);
        vis.insert(startGene);
        while(!q.empty())
        {
            ++ret;
            int size = q.size();
            for(int i = 0; i < size; ++i)
            {
                string t = q.front();
                q.pop();
                for(int j = 0; j < 8; ++j)
                {
                    for(int k = 0; k < 4; ++k)
                    {
                        string tmp = t;
                        tmp[j] = chargeArr[k];
                        if(bankHash.count(tmp) && !vis.count(tmp)) // 有效的且没访问过
                        {
                            if(tmp == endGene)
                                return ret;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return -1;
    }
};